package wallet

import (
	"context"
	"go.uber.org/zap"
	gamePb "navi-mumbai/app/rpc/game/pb"
	"navi-mumbai/app/rpc/wallet/pb"

	"navi-mumbai/app/api/game/internal/svc"
	"navi-mumbai/app/api/game/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderLogic {
	return &UserOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserOrderLogic) UserOrder(req *types.UserOrderReq) (resp *types.UserOrderResp, err error) {
	// 查询openId
	var in gamePb.GetUidByOpenIdReq
	in.OpenId = req.OpenId
	in.ProviderId = req.ProviderId
	var rsp *gamePb.GetUidByOpenIdResp
	rsp, err = l.svcCtx.GameUserRpc.GetUidByOpenId(l.ctx, &in)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}

	var in2 pb.UserOrderReq
	in2 = pb.UserOrderReq{
		Uid:            rsp.Uid,
		MoneyType:      req.MoneyType,
		MoneyAmount:    req.MoneyAmount,
		ExternalFlowNo: req.ExternalFlowNo,
		GameId:         req.GameId,
		ProviderId:     req.ProviderId,
		OrderTime:      req.OrderTime,
	}

	var rsp2 *pb.UserOrderResp
	rsp2, err = l.svcCtx.WalletRpc.UserOrder(l.ctx, &in2)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return &types.UserOrderResp{}, err
	}
	resp = &types.UserOrderResp{
		FlowNo:    rsp2.FlowNo,
		Balance:   rsp2.Balance,
		MoneyType: rsp2.MoneyType,
	}
	return
}
